from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAdminUser

from goods.models import GoodsChannel, GoodsChannelGroup, GoodsCategory
from meiduo_admin.serializers.channels import GoodsChannelSerializer, ChannelTypesSerializer


class ChannelTypesView(ListAPIView):
    permission_classes = [IsAdminUser]
    queryset = GoodsChannelGroup.objects.all()
    serializer_class = ChannelTypesSerializer
    pagination_class = None


class GoodsCategoriesView(ListAPIView):
    permission_classes = [IsAdminUser]
    queryset = GoodsCategory.objects.filter(parent=None)
    serializer_class = ChannelTypesSerializer
    pagination_class = None


class GoodsChannelViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]
    # 指定router 动态生成路由时, 提取参数的正则表达式
    lookup_value_regex = r'\d+'
    serializer_class = GoodsChannelSerializer
    queryset = GoodsChannel.objects.all()
